package com.ordering.utils;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class WindowLocationUtils {

    public static void setWindowCenter(Component cp) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        cp.setLocation(screenSize.width / 2 - (cp.getWidth() / 2),
                screenSize.height / 2 - (cp.getHeight() / 2));
    }

    public static void hideColumn(JTable table, int index) {
        TableColumn tc = table.getColumnModel().getColumn(index);
        tc.setMaxWidth(0);
        tc.setPreferredWidth(0);
        tc.setWidth(0);
        tc.setMinWidth(0);

        table.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
        table.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
    }

    public static void disabledAllComponent(JDialog parent) {
        Component[] components = parent.getComponents();
        for (Component component : components) {
            component.setEnabled(false);
        }
    }

    public static void enabledAllComponent(JDialog parent) {
        Component[] components = parent.getComponents();
        for (Component component : components) {
            component.setEnabled(true);
        }
    }

    public static void copyFile(String srcFileName, String dstFileName) {
        FileChannel srcChannel = null;
        FileChannel dstChannel = null;
        try {
            srcChannel = new FileInputStream(srcFileName).getChannel();
            dstChannel = new FileOutputStream(dstFileName).getChannel();
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WindowLocationUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(WindowLocationUtils.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                srcChannel.close();
            } catch (IOException ex) {
                Logger.getLogger(WindowLocationUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                dstChannel.close();
            } catch (IOException ex) {
                Logger.getLogger(WindowLocationUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
